<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-17 22:48:58
 * @LastEditTime: 2022-05-19 10:46:35
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
-->
<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-02-03 21:51:15
 * @LastEditTime: 2022-05-17 11:28:15
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
-->
<template>
  <div class="user-manage-container">
    <el-card>
      <el-button
        type="primary"
        @click="onImportExcelClick"
        v-permission="['importUser']"
      >{{ $t('excel.importExcel') }}</el-button>
      <el-button
        type="success"
        @click="onExportExcelClick"
      >{{ $t('excel.exportExcel') }}</el-button>
      <Export2Excel v-model="modelValue"></Export2Excel>
    </el-card>
    <el-card style="margin-top:10px">
      <el-table
        :data="tableData"
        border
        style="width:100%"
      >
        <el-table-column
          label="#"
          type="index"
        ></el-table-column>
        <el-table-column
          prop="username"
          :label="$t('excel.name')"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          :label="$t('excel.mobile')"
        ></el-table-column>
        <el-table-column
          :label="$t('excel.avatar')"
          align="center"
        >
          <template #default="{ row }">
            <el-image
              class="avatar"
              :src="row.avatar"
              :preview-src-list="[row.avatar]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column :label="$t('excel.role')">
          <template #default="{ row }">
            <div v-if="row.role && row.role.length > 0">
              <el-tag
                v-for="item in row.role"
                :key="item.id"
                size="mini"
              >
                {{
                  item.title
                }}
              </el-tag>
            </div>
            <div v-else>
              <el-tag size="mini">{{ $t('excel.defaultRole') }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('excel.openTime')">
          <template #default="{ row }">{{ $filters.formatDate(row.openTime) }}</template>
        </el-table-column>
        <el-table-column
          :label="$t('excel.action')"
          fixed="right"
          width="260"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              size="mini"
              @click="onShowClick(row._id)"
            >{{ $t('excel.show') }}</el-button>
            <el-button
              type="info"
              size="mini"
              @click="onShowRoleClick(row._id)"
              v-permission="['distributeRole']"
            >{{ $t('excel.showRole') }}</el-button>
            <el-button
              type="danger"
              size="mini"
              @click="onRemoveClick(row)"
              v-permission="['distributeRole']"
            >{{ $t('excel.remove') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        :total="total"
        :current-page="page"
        :page-size="size"
        :page-sizes="[2, 5, 10, 20]"
        layout="total, sizes, prev, pager, next, jumper"
      ></el-pagination>
    </el-card>
    <Role
      v-model='visible'
      :userId="currentUserId"
      @update='getListData'
    />
  </div>
</template>

<script setup>
import { getUserManagerList, deleteUser } from '@/api/user-manager'
import { watchSwitchLang } from '@/utils/i18n'
import { ElMessageBox, ElMessage } from 'element-plus'
import { ref, onActivated } from 'vue'
import { useI18n } from 'vue-i18n'
import { useRouter } from 'vue-router'
import Export2Excel from './components/Export2Excel.vue'
import Role from './components/Role'
const router = useRouter()
// 数据相关
const tableData = ref([])
const total = ref(10)
const page = ref(1)
const size = ref(2)
// 获取列表数据方法
const getListData = async () => {
  const res = await getUserManagerList({
    page: page.value,
    size: size.value
  })
  tableData.value = res.list
  total.value = res.total
}
getListData()
onActivated(getListData)
watchSwitchLang(getListData)
// 删除用户
const i18n = useI18n()
const onRemoveClick = async (row) => {
  try {
    await ElMessageBox.confirm(i18n.t('excel.dialogTitle1') + row.username + i18n.t('excel.dialogTitle2'),
      { type: 'warning' })
    await deleteUser(row._id)
    ElMessage.success(i18n.t('excel.removeSuccess'))
    getListData()
  } catch (e) {
    console.log(e)
  }
}
// 查看角色
const visible = ref(false)
const currentUserId = ref(0)
const onShowRoleClick = (id) => {
  currentUserId.value = id
  visible.value = true
}
const onShowClick = (id) => {
  router.push(`/user/info/${id}`)
}
// 操作分页
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData()
}
const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData()
}
// 跳转文件上传
const onImportExcelClick = () => {
  router.push('/user/import')
}
// 文件導出
const modelValue = ref(false)
const onExportExcelClick = () => {
  modelValue.value = true
}
</script>

<style lang="scss" scoped>
.user-manage-container {
  .header {
    margin-bottom: 22px;
    text-align: right;
  }
  :deep(.avatar) {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }

  :deep(.el-tag) {
    margin-right: 6px;
  }

  :deep(.pagination) {
    margin-top: 20px;
    text-align: center;
    justify-content: center;
  }
}
</style>
